package com.jinlejia.erp.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.databinding.ItemHouseInnerBinding;
import com.jinlejia.erp.ui.house.HouseDetailActivity;
import com.jinlejia.erp.utils.Arith;
import com.jinlejia.erp.utils.StringUtil;

import java.util.ArrayList;
import java.util.Map;


/**
 * 带看-房源列表adapter
 */

public class AccessInnerAdapter extends YfListAdapter<Map<String, Object>> {


    private Context context;
    boolean isSale;

    public AccessInnerAdapter(ArrayList<Map<String, Object>> data,boolean isSale) {
        super(data);
        this.isSale = isSale;
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_house_inner, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        ItemHouseInnerBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        final Map<String, Object> map = mData.get(i);
        Glide.with(context).load(ApiConfig.MAGEHEADER_URL() + map.get("imageUrl")).placeholder(R.mipmap.ic_no_img).error(R.mipmap.ic_no_img).into(binding.shootIcon);
        binding.address.setText(map.get("estate") + "  " + map.get("shsHouseType"));
        if (!isSale) {
            binding.data.setText( String.valueOf(map.get("county")).concat("-").concat(String.valueOf(map.get("area"))).concat("|").concat( String.valueOf(map.get("square"))).concat("㎡") );
            binding.money.setText(map.get("salePrice") + "元");
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, HouseDetailActivity.class);
                    intent.putExtra("type", "rent");
                    intent.putExtra("code", map.get("rscode").toString());
                    context.startActivity(intent);
                }
            });
        } else {
            binding.data.setText( String.valueOf(map.get("county")).concat("-").concat(String.valueOf(map.get("area"))).concat("|").concat(String.valueOf(map.get("square")))
                    .concat("㎡|").concat(Arith.div(StringUtil.valueOf(map.get("salePrice")),StringUtil.valueOf(map.get("square")),2)).concat("万/㎡"));
            binding.money.setText(map.get("salePrice") + "万");
           viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   Intent intent = new Intent(context, HouseDetailActivity.class);
                   intent.putExtra("type", "shs");
                   intent.putExtra("code", map.get("rscode").toString());
                   context.startActivity(intent);
               }
           });
        }
        binding.lookAtHose.setText(String.valueOf(map.get("houseType")));
        binding.feedback.setText(String.valueOf(map.get("feedback")));

        binding.getRoot().setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }


    static final class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
}
